
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T2 - The original code has been modified in order to add interactions with the gender dummy.
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************

cap prog drop maketable_t2
prog define maketable_t2
est clear
syntax varlist [if], [Table(string)] [SPEC11(varlist)] [SPEC21(varlist)]   [SPEC12(varlist)] [SPEC22(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [HET(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [NUSPEC(string)] [REG][POSTHEADPA(string)] [POSTHEADPB(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	gen Winner_Male = Winner * Gender_Followup
	

	*****************************
	*CONTROLS CLEAN UP
	*****************************
	*Another version of Winner that can be dropped for the control regressions since winner has no interpretation
		cap drop Winner_alt
		gen Winner_alt=Winner
		gen Winner_Male_alt = Winner_Male
	
	label var Winner_Male "Male Winner"
	label var Winner_Male_alt "Male Winner"
	
	cap drop _*
	cap drop *AA* 
	
	local k=1
	if "`cont'"!="" {
	foreach var of local cont {
		sum `var'
		g AA`k'= `var'
		sum AA`k'
			local ++k
		}
	}

	foreach var of varlist AA*{
		sum `var'
		gen miss_`var'=`var'==.
		replace `var'=r(mean) if miss_`var'==1
		gen Winner_`var'=Winner*`var'
		gen mi_Winner_`var'=Winner*miss_`var'
		}

	local controls_final "Winner_AA* mi_Winner_AA*"
	
	*****************************
	*REGRESSION- LINEAR IN RANK SPEC 1
	*****************************
	
	local ester=1
	local eststr ""

	
	forvalues i=1/4 {
		if strpos("`spec1`i''", "") { 
			local nuspec1 "`i'"
			} 
		}
	
	foreach out in `varlist'  {		
	
		forvalues i=1/`nuspec1'{
			
			
			******************************	
			*LINEAR SPEC WITHOUT CONTROLS 
			******************************
	
			xtreg `out'   `spec1`i'' Winner_Quint_Rank_NS Winner Winner_Male i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				
				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
			
			******************************	
			*TERCILE SPEC WITH CONTROLS
			******************************
		
			xtreg `out'   `spec1`i'' Winner_Quint_Rank_NS Winner_Male_alt Winner_alt `controls_final' i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)


				est store E`ester'
				local eststr "`eststr' E`ester'"
				local ++ester
				
			}
		}
		
		local part2=`ester'+1
		
	esttab `eststr' using "`table'",  nostar     ///
		keep(`spec11' Winner_Quint_Rank_NS Winner_Male Winner) noobs label noabbrev `type' replace nogaps ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead("`postheadpa'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'")  ///
		postfoot( )


	*****************************
	*REGRESSION- TERCILES RANK SPEC 2
	*****************************

	local ester=1
	local eststr ""
		
	forvalues i=1/4 {
		if strpos("`spec2`i''", "") { 
			local nuspec2 "`i'"
			} 
		}
		
	foreach out in `varlist'  {		
		
		forvalues i=1/`nuspec2'{

		
			******************************	
			*TERCILE SPEC WITHOUT CONTROLS 
			******************************
			
			xtreg `out' `spec2`i'' Winner_Quint_Rank_NS_Tercile_3 Winner_Quint_Rank_NS_Tercile_2 Winner Winner_Male i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)

				estadd local Lasso " "

				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 
					estadd scalar No_Obs=e(N_g)
					local no_obs=e(N_g)

				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
										
				qui sum `out' if Winner==0 & Gender_Followup==1
					estadd scalar Cont_Mean_Male=r(mean)
					estadd scalar Cont_Sd_Male =r(sd)
				
				est store E`ester'

				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  Winner_Quint_Rank_NS_Tercile_3= Winner_Quint_Rank_NS_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
				
				
				test  `het'_Tercile_3 + Winner_Quint_Rank_NS_Tercile_3 = `het'_Tercile_2 + Winner_Quint_Rank_NS_Tercile_2
				
						local n3= r(p)
						local star ""
							if `n3'<=.01 local star "\sym{***}"
							if `n3'>.01 & `n3'<=.05 local star "\sym{**}"	
							if `n3'>.05 & `n3'<=.1 local star "\sym{*}"	
						local n4 = trim("`: display %10.3f `n3''")
						local combo "`n4'"
							estadd local p5 `combo'	
					
				local stats11 "space   p4 space space space p5 space "
				local stats21 " 0  2 2 2 2 2 2 "
				local stats31 " @  @ @ @ @ @ @"
				local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
				local stats61 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Male Winner*Top Tercile Rank + Winner*Top Tercile Rank=" "\hspace{0.1in}Male Winner*Middle Tercile Rank + Winner*Middle Tercile Rank"   "

			******************************	
			*TERCILE SPEC WITH CONTROLS
			******************************
			
			xtreg `out'   `spec2`i'' Winner_Male_alt Winner_alt Winner_Quint_Rank_NS_Tercile_3 Winner_Quint_Rank_NS_Tercile_2 `controls_final' i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) fe i(Id)


				estadd local Lasso "X"
				estadd scalar N2=e(N)
				distinct Id `if' & `out'!=. 

					estadd scalar No_Obs=`no_obs'
				
				estadd local space=""
				qui sum `out' if Winner==0
					estadd scalar Cont_Mean=r(mean)
					estadd scalar Cont_Sd=r(sd)
				
				qui sum `out' if Winner==0 & Gender_Followup==1
					estadd scalar Cont_Mean_Male=r(mean)
					estadd scalar Cont_Sd_Male =r(sd)	
				
				est store E`ester'
				
				local eststr "`eststr' E`ester'"
				local ++ester
				
				test  Winner_Quint_Rank_NS_Tercile_3= Winner_Quint_Rank_NS_Tercile_2
				
						local n1= r(p)
						local star ""
							if `n1'<=.01 local star "\sym{***}"
							if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
							if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
						local n2 = trim("`: display %10.3f `n1''")
						local combo "`n2'"
							estadd local p4 `combo'	
				
				
				test  `het'_Tercile_3 + Winner_Quint_Rank_NS_Tercile_3 = `het'_Tercile_2 + Winner_Quint_Rank_NS_Tercile_2
				
						local n3= r(p)
						local star ""
							if `n3'<=.01 local star "\sym{***}"
							if `n3'>.01 & `n3'<=.05 local star "\sym{**}"	
							if `n3'>.05 & `n3'<=.1 local star "\sym{*}"	
						local n4 = trim("`: display %10.3f `n3''")
						local combo "`n4'"
							estadd local p5 `combo'	
						
				local stats11 "space   p4 space space space p5 space"
				local stats21 " 0  2 2 2 2 2 2"
				local stats31 " @  @ @ @ @ @ @"
				local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile Rank=" "\hspace{0.1in}Winner*Middle Tercile Rank"   "
				local stats61 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Male Winner*Top Tercile Rank + Winner*Top Tercile Rank=" "\hspace{0.1in}Male Winner*Middle Tercile Rank + Winner*Middle Tercile Rank"   "

				
			}
			}
		local part2=`ester'+1
		
	esttab `eststr' using "`table'", nostar nonumbers nomtitles nostar order( `het'_Tercile_3 `het'_Tercile_2 `spec`i'' Winner_Quint_Rank_NS_Tercile_3 Winner_Quint_Rank_NS_Tercile_2 Winner Winner_Male Gender_Followup) ///
		keep( `spec21' Winner Winner_Male Winner_Quint_Rank_NS_Tercile_3 Winner_Quint_Rank_NS_Tercile_2) label noabbrev `type' append nogaps ///
		stats( `stats11' space  Cont_Mean Cont_Sd Cont_Mean_Male Cont_Sd_Male Lasso N2 No_Obs, fmt( `stats21' 2 2 2  2 2 0 0) layout( `stats31' @ @  "[@]" @   "[@]"   @ @) /// 
		labels(  `stats41'  " "  `stats61'  " " "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " " "  \hspace{-.1 in} Mean of Outcome for Male Grant Losers" " " "\hspace{-.1 in} Controls" " \hspace{-.1 in} N" " \hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.2f) se(%9.2f) noline posthead(`postheadpb') subs("\beta\_" "\beta_") /// 
		prehead( )  ///
		postfoot( `postfoot1' \hline \hline \end{tabular} } \begin{tablenotes}[flushleft] \small  \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		
	drop Winner_alt *AA*

	end
	
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
*CODE FOR TABLES: T4 - The original code has been modified in order to add interactions with the gender dummy.
*********************************************************************************************************************************************************
*********************************************************************************************************************************************************
		
cap prog drop maketable_t4_2
prog define maketable_t4_2
est clear
syntax varlist [if], [Table(string)] [SPEC1(varlist)] [SPEC2(varlist)] [SPEC3(varlist)] [SPEC4(varlist)]  [CONT(varlist)] [MAIN(varlist)]  [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]  [REG] [POSTHEAD(string)] [POSTFOOT1(string)] [PREHEAD3(string)]

	local ester=1
	local eststr ""

	local cmd "xtreg"
	if "`reg'"!="" local cmd "reg"

	local fe "fe i(Id)"
	if "`reg'"!="" local fe ""
	
		forvalues j=1/4{
		    
			local out: word `j' of `varlist'
			
			gen Winner_MR_Prediction_Obs_T3=Winner_MR_Prediction`j'_Obs_T3
			gen Winner_MR_Predict_Obs_Rank_T3=Winner_MR_Predict`j'_Obs_Rank_T3
		    gen Winner_MR_Prediction_Obs_T2=Winner_MR_Prediction`j'_Obs_T2
			gen Winner_MR_Predict_Obs_Rank_T2=Winner_MR_Predict`j'_Obs_Rank_T2
				
			gen Male_W_MR_Prediction_Obs_T3 = Gender_Followup * Winner_MR_Prediction`j'_Obs_T3
			gen Male_W_MR_Predict_Obs_Rank_T3 = Gender_Followup * Winner_MR_Predict`j'_Obs_Rank_T3
		    gen Male_W_MR_Prediction_Obs_T2 = Gender_Followup * Winner_MR_Prediction`j'_Obs_T2
			gen Male_W_MR_Predict_Obs_Rank_T2 = Gender_Followup * Winner_MR_Predict`j'_Obs_Rank_T2
			

			xi: `cmd' `out' `main' `cont' Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Male_W_MR_Prediction_Obs_T3 Male_W_MR_Prediction_Obs_T2 Winner Winner_Male i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) `fe'
		
					test Winner_MR_Prediction_Obs_T3=Winner_MR_Prediction_Obs_T2

							local n1= r(p)
							local star ""
								if `n1'<=.01 local star "\sym{***}"
								if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
								if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
							local n2 = trim("`: display %10.3f `n1''")
							local combo "`n2'"
								estadd local p4 `combo'	
					
					
					test  Male_W_MR_Prediction_Obs_T3 + Winner_MR_Prediction_Obs_T3 = Male_W_MR_Prediction_Obs_T2 + Winner_MR_Prediction_Obs_T2
				
						local n3= r(p)
							local star ""
								if `n3'<=.01 local star "\sym{***}"
								if `n3'>.01 & `n3'<=.05 local star "\sym{**}"	
								if `n3'>.05 & `n3'<=.1 local star "\sym{*}"	
							local n4 = trim("`: display %10.3f `n3''")
							local combo "`n4'"
								estadd local p5 `combo'	
							
							
					estadd scalar N2=e(N)
						distinct Id `if' & `out'!=. 
							estadd scalar No_Obs=e(N_g)
							
							estadd local space=""
							qui sum `out' if Winner==0
								estadd scalar Cont_Mean=r(mean)
								estadd scalar Cont_Sd=r(sd)
							qui sum `out' if Winner==0 & Gender_Followup ==1
								estadd scalar Cont_Mean_Male=r(mean)
								estadd scalar Cont_Sd_Male=r(sd)
								
								est store E`ester'
							
							local eststr "`eststr' E`ester'"
							local ++ester
									
						local stats11 "space   p4 space space space p5 space "
						local stats21 " 0  2 2 2 2 2 2 "
						local stats31 " @  @ @ @ @ @ @"
						local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile=" "\hspace{0.1in}Winner*Middle Tercile"   "	
						local stats61 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner_Male*Top Tercile + Winner*Top Tercile=" "\hspace{0.1in}Winner_Male*Middle Tercile + Winner*Middle Tercile "   "	

	
			xi: `cmd' `out' `main' `cont' Winner_MR_Predict_Obs_Rank_T3 Winner_MR_Predict_Obs_Rank_T2 Male_W_MR_Predict_Obs_Rank_T3 Male_W_MR_Predict_Obs_Rank_T2 Winner Winner_Male i.Surveyor_Code i.Survey_Version i.survey_month `if'  [weight=Propensity_Score], clu(GroupNumber) `fe'

			
					test Winner_MR_Predict_Obs_Rank_T3=Winner_MR_Predict_Obs_Rank_T2
			
							local n1= r(p)
							local star ""
								if `n1'<=.01 local star "\sym{***}"
								if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
								if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
							local n2 = trim("`: display %10.3f `n1''")
							local combo "`n2'"
								estadd local p4 `combo'	
								
								
					test  Male_W_MR_Predict_Obs_Rank_T3 + Winner_MR_Predict_Obs_Rank_T3 = Male_W_MR_Predict_Obs_Rank_T2 + Winner_MR_Predict_Obs_Rank_T2
				
						local n3= r(p)
							local star ""
								if `n3'<=.01 local star "\sym{***}"
								if `n3'>.01 & `n3'<=.05 local star "\sym{**}"	
								if `n3'>.05 & `n3'<=.1 local star "\sym{*}"	
							local n4 = trim("`: display %10.3f `n3''")
							local combo "`n4'"
								estadd local p5 `combo'		
								
								
					estadd scalar N2=e(N)
						distinct Id `if' & `out'!=. 
							estadd scalar No_Obs=e(N_g)
							
							estadd local space=""
							qui sum `out' if Winner==0
								estadd scalar Cont_Mean=r(mean)
								estadd scalar Cont_Sd=r(sd)
							qui sum `out' if Winner==0 & Gender_Followup ==1
								estadd scalar Cont_Mean_Male=r(mean)
								estadd scalar Cont_Sd_Male=r(sd)
								
								est store E`ester'
							
							local eststr "`eststr' E`ester'"
							local ++ester
	
						local stats11 "space   p4 space space space p5 space "
						local stats21 " 0  2 2 2 2 2 2 "
						local stats31 " @  @ @ @ @ @ @"
						local stats41 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner*Top Tercile=" "\hspace{0.1in}Winner*Middle Tercile"   "	
						local stats61 " "  \hspace{0.013in} \it{P-value from F-Test }"  "\hspace{0.1in}Winner_Male*Top Tercile + Winner*Top Tercile=" "\hspace{0.1in}Winner_Male*Middle Tercile + Winner*Middle Tercile "   "	

						
			drop Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2 Male_W_MR_Prediction_Obs_T3 Male_W_MR_Prediction_Obs_T2 Male_W_MR_Predict_Obs_Rank_T3  Male_W_MR_Predict_Obs_Rank_T2
			
		}
		local part2=`ester'+1
esttab `eststr' using "`table'",   nostar    ///
		keep (Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Male_W_MR_Prediction_Obs_T3 Male_W_MR_Prediction_Obs_T2  Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2 Male_W_MR_Predict_Obs_Rank_T3  Male_W_MR_Predict_Obs_Rank_T2 Winner Winner_Male)  ///
		order(Winner_MR_Prediction_Obs_T3 Winner_MR_Prediction_Obs_T2 Male_W_MR_Prediction_Obs_T3 Male_W_MR_Prediction_Obs_T2  Winner_MR_Predict_Obs_Rank_T3  Winner_MR_Predict_Obs_Rank_T2 Male_W_MR_Predict_Obs_Rank_T3  Male_W_MR_Predict_Obs_Rank_T2 Winner Winner_Male) label noabbrev `type' replace nogaps ///
		drop(`cont') ///
		coeflabels ( Winner_MR_Prediction_Obs_T3 "\specialcell{\enspace Winner*Top Tercile Controls}" ///
		Winner_MR_Prediction_Obs_T2 "\specialcell{\enspace Winner*Top Middle Controls}" ///
		Winner_MR_Predict_Obs_Rank_T3 "\specialcell{\enspace Winner*Top Tercile Controls+Rank}"  ///
		Winner_MR_Predict_Obs_Rank_T2 "\specialcell{\enspace Winner*Top Middle Controls+Rank}" ///
		Male_W_MR_Prediction_Obs_T3 "\specialcell{\enspace Male Winner*Top Tercile Controls}" ///
		Male_W_MR_Prediction_Obs_T2 "\specialcell{\enspace Male Winner*Top Middle Controls}" ///
		Male_W_MR_Predict_Obs_Rank_T3 "\specialcell{\enspace Male Winner*Top Tercile Controls+Rank}"  ///
		Male_W_MR_Predict_Obs_Rank_T2 "\specialcell{\enspace Male Winner*Top Middle Controls+Rank}") ///
	stats( `stats11' space  Cont_Mean Cont_Sd Cont_Mean_Male Cont_Sd_Male N2 No_Obs, fmt( `stats21' 2 2 2  2 2 0 0) layout( `stats31' @ @  "[@]" @  "[@]"  @ @) /// 
		labels(  `stats41'  " "  `stats61'  " "  "\hline \hspace{-.1 in} Mean of Outcome for Grant Losers" " " "\hspace{-.1 in} Mean of Outcome for Male Grant Losers" " " "\hspace{-.1 in} N" "\hspace{-.1 in} No. HHs")) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead("`posthead'") subs("\beta\_" "\beta_")  ///
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead3' `prehead1'`ester'`prehead2'") /// 
		postfoot( `postfoot1' \hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end	
		

***************************************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************************************
*CODE FOR TABLES: A17 - The original code has been modified in order to create a table desplaying values for the male only subsample and a table desplaying values for the female only subsample.
***************************************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************************************
*** FOR MALES:
cap prog drop baltest3
prog define baltest3
est clear
syntax [if], PANELA(varlist) [PANELB(varlist)] [PANELC(varlist)] [PANELD(varlist)] [Table(string)] [CONT(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [GPLEVELPANEL(string)] [SPECIALCONTVARS(varlist)] [SPECIALCONT(varlist)] [REFERENCE(string)] [TREATMENT(varlist)]

  
	
	global suestestimates 
	global suesttest 	
	global ftest
	

	foreach cov in DVmean   Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  {

	foreach var in `panela' `panelb' `panelc' `paneld' {
		
		
		areg `var' Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3 i.Survey_Version i.survey_month  `cont' `if' `weight', clu(GroupNumber) a(Final_Randomization_Cluster) 

		
		if  "`cov'"!="N" & "`cov'"!="DVmean`j'" {
			local b= _b[`cov']
			local n1= r(p)
			local se = _se[`cov']
			
// 			local star ""
// 				if `n1'<=.01 local star "\sym{***}"
// 				if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
// 				if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
			local n2 = trim("`: display %10.3f `b''") 
			local `var'_combo "`n2'"
			
			local se = trim("`: display %10.3f `se''") 
			local `var'_se "(`se')"
			
					
			}

		if "`cov'"=="N" {
			local b= e(N)
			local n1=500
			local n2 = trim("`: display %10.0f `b''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
		sum `var' if Quint_Rank_NS_Tercile_1==1 & Gender_Followup == 1
		if "`cov'"=="DVmean`j'" {
			local mn=r(mean)
			local n2 = trim("`: display %10.3f `mn''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
	
	

}
foreach var in `panela' `panelb' `panelc' `paneld' {
	estadd local b_`var' ``var'_combo' 
	estadd local se_`var' ``var'_se'
}

estadd local PA ""
estadd local PB ""
estadd local PC ""
estadd local PD ""

est store `cov'
}



local cellsA "" 
foreach var in `panela' {
    local cellsA "`cellsA' b_`var' se_`var'"
}
local cellsB "" 
foreach var in `panelb' {
    local cellsB "`cellsB' b_`var' se_`var'"
}
local cellsC "" 
foreach var in `panelc' {
    local cellsC "`cellsC' b_`var' se_`var'"
}
local cellsD "" 
foreach var in `paneld' {
    local cellsD "`cellsD' b_`var' se_`var'"
}

	
	
local labstr "\textit{Panel A: Individual Characteristics of Ranked Entrepreneur}"
	foreach out in `panela' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
if "`panelb'"!="" {
	local labstr `"`labstr'" "\textit{Panel B: Sector of Ranked Entrepreneur}"'
	foreach out in `panelb' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`panelc'"!="" {
	local labstr `"`labstr'" "\textit{Panel C: Household Characteristics}"'
	foreach out in `panelc' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`paneld'"!="" {
	local labstr `"`labstr'" "\textit{Panel D: Characteristics of Household Businesses}"'
	foreach out in `paneld' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}


di "`labstr'"
di "`panelc'"
	
	esttab DVmean    Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  using "`table'", nostar noabbrev `type' replace nogaps /// 
		mlab("\specialcell{Bottom \\ Tercile\\ Rank \\ Mean}"  "\specialcell{Middle \\ Tercile\\ Rank \\Difference}" "\specialcell{Top \\ Tercile\\ Rank \\Difference}"   ) /// 
	stats(PA `cellsA' PB `cellsB' PC `cellsC' PD `cellsD', labels("`labstr'")) keep( ) ///
		nonote   noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'5`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		end
	
			
*** FOR FEMALES:	
	cap prog drop baltest3b
prog define baltest3b
est clear
syntax [if], PANELA(varlist) [PANELB(varlist)] [PANELC(varlist)] [PANELD(varlist)] [Table(string)] [CONT(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)] [GPLEVELPANEL(string)] [SPECIALCONTVARS(varlist)] [SPECIALCONT(varlist)] [REFERENCE(string)] [TREATMENT(varlist)]

  
	
	global suestestimates 
	global suesttest 	
	global ftest
	

	foreach cov in DVmean   Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  {

	foreach var in `panela' `panelb' `panelc' `paneld' {
		
		
		areg `var' Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3 i.Survey_Version i.survey_month  `cont' `if' `weight', clu(GroupNumber) a(Final_Randomization_Cluster) 

		
		if  "`cov'"!="N" & "`cov'"!="DVmean`j'" {
			local b= _b[`cov']
			local n1= r(p)
			local se = _se[`cov']
			
// 			local star ""
// 				if `n1'<=.01 local star "\sym{***}"
// 				if `n1'>.01 & `n1'<=.05 local star "\sym{**}"	
// 				if `n1'>.05 & `n1'<=.1 local star "\sym{*}"	
			local n2 = trim("`: display %10.3f `b''") 
			local `var'_combo "`n2'"
			
			local se = trim("`: display %10.3f `se''") 
			local `var'_se "(`se')"
			
					
			}

		if "`cov'"=="N" {
			local b= e(N)
			local n1=500
			local n2 = trim("`: display %10.0f `b''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
		sum `var' if Quint_Rank_NS_Tercile_1==1 & Gender_Followup == 0
		if "`cov'"=="DVmean`j'" {
			local mn=r(mean)
			local n2 = trim("`: display %10.3f `mn''")
			local `var'_combo "`n2'"
			local `var'_se " "
			}
	
	

}
foreach var in `panela' `panelb' `panelc' `paneld' {
	estadd local b_`var' ``var'_combo' 
	estadd local se_`var' ``var'_se'
}

estadd local PA ""
estadd local PB ""
estadd local PC ""
estadd local PD ""

est store `cov'
}



local cellsA "" 
foreach var in `panela' {
    local cellsA "`cellsA' b_`var' se_`var'"
}
local cellsB "" 
foreach var in `panelb' {
    local cellsB "`cellsB' b_`var' se_`var'"
}
local cellsC "" 
foreach var in `panelc' {
    local cellsC "`cellsC' b_`var' se_`var'"
}
local cellsD "" 
foreach var in `paneld' {
    local cellsD "`cellsD' b_`var' se_`var'"
}

	
	
local labstr "\textit{Panel A: Individual Characteristics of Ranked Entrepreneur}"
	foreach out in `panela' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
if "`panelb'"!="" {
	local labstr `"`labstr'" "\textit{Panel B: Sector of Ranked Entrepreneur}"'
	foreach out in `panelb' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`panelc'"!="" {
	local labstr `"`labstr'" "\textit{Panel C: Household Characteristics}"'
	foreach out in `panelc' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}
if "`paneld'"!="" {
	local labstr `"`labstr'" "\textit{Panel D: Characteristics of Household Businesses}"'
	foreach out in `paneld' {
		local tit: variable label `out'
		local labstr `"`labstr'" "`tit'"  " "'
		}
}


di "`labstr'"
di "`panelc'"
	
	esttab DVmean    Quint_Rank_NS_Tercile_2 Quint_Rank_NS_Tercile_3  using "`table'", nostar noabbrev `type' replace nogaps /// 
		mlab("\specialcell{Bottom \\ Tercile\\ Rank \\ Mean}"  "\specialcell{Middle \\ Tercile\\ Rank \\Difference}" "\specialcell{Top \\ Tercile\\ Rank \\Difference}"   ) /// 
	stats(PA `cellsA' PB `cellsB' PC `cellsC' PD `cellsD', labels("`labstr'")) keep( ) ///
		nonote   noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'5`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})
		end
		
		

***************************************************************************************************************************************************************************************************************************************************
*****************************************************************************************************************************************************************************************************************************************************
*CODE FOR TABLES: A21 - The original code has been modified in order to create three tables: 1) the first program (maketable_hetero_incpubM) creates a table that displays the values for the male only subsample, 2) the second program (maketable_hetero_incpubF) creates a table that displays the values for the female only subsample, and 3) the third program (maketable_hetero_incpub_interF) replicates the original table by adding interactions with the gender dummy.
***************************************************************************************************************************************************************************************************************************************************
***************************************************************************************************************************************************************************************************************************************************		

*** FOR MALES:
cap prog drop maketable_hetero_incpubM
prog define maketable_hetero_incpubM
est clear
syntax varlist [if], [Table(string)] [MAIN(varlist)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	

	local ester=1
	local eststr ""
	tokenize `ologit'
	
	local spec1 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=."
	local spec2 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==1"
	local spec3 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==0"

	
	local names ""Pooled""Stakes"  "No Stakes""
					
	foreach out in `varlist'  {		
	
		foreach c in `main'  {	
			forvalues i=1/3{
			cap drop Characteristic  CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
			gen Characteristic=`c'
			gen CharacteristicPublicIncentives=Characteristic*Public*Incentives
			gen CharacteristicPublic=Characteristic*Public
			gen CharacteristicIncentives=Characteristic*Incentives
		
			local lab: word `i' of `names'


			xi: areg `out' `spec`i'' & q>=3 & q<=8 & R_Gender_Followup == 1, clu(GroupNumber)a(Final_Randomization_Cluster)
		
		estadd scalar N2=e(N)

		estadd local Treatment "`lab'"
		local lab:  variable label `c' 
		estadd local sample="`lab'"
		
		sum `out' if R_Gender_Followup == 1
		estadd scalar Control_Mean=r(mean)
		estadd scalar Control_SD=r(sd)

		
		distinct Id if `out'!=. & R_Gender_Followup == 1
		estadd scalar N3=r(ndistinct)
		
		estadd local space=""


		est store E`ester'
		
		local eststr "`eststr' E`ester'"
		local ++ester
		}
		drop CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
		
		}
		}
		local part2=`ester'+1
esttab `eststr' using "`table'", nostar keep(CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives ///
  Characteristic  ) label noabbrev `type' replace nogaps ///
		coeflabels(CharacteristicPublicIncentives "Characteristic*Public*Incentives"  CharacteristicIncentives "Characteristic*Incentives" ///
		CharacteristicPublic "Characteristic*Public" Characteristic   "Characteristic" ///
		Public "Public" Incentives "Incentives") ///
			stats( space Control_Mean Control_SD space sample Treatment space N2 N3, fmt( 2 2 2 2 0 0) layout( @ @ [@] @ @ @) /// 
		labels(" " "Mean of" "Outcome" " " "Characteristic" "Treatment" " " "N" "No. HHs" )) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1.3\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end					


*** FOR FEMALES:
cap prog drop maketable_hetero_incpubF
prog define maketable_hetero_incpubF
est clear
syntax varlist [if], [Table(string)] [MAIN(varlist)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	

	local ester=1
	local eststr ""
	tokenize `ologit'
	
	local spec1 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=."
	local spec2 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==1"
	local spec3 "Characteristic  CharacteristicPublic CharacteristicIncentives CharacteristicPublicIncentives   Public_Incentives Public Incentives i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==0"

	
	local names ""Pooled""Stakes"  "No Stakes""
					
	foreach out in `varlist'  {		
	
		foreach c in `main'  {	
			forvalues i=1/3{
			cap drop Characteristic  CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
			gen Characteristic=`c'
			gen CharacteristicPublicIncentives=Characteristic*Public*Incentives
			gen CharacteristicPublic=Characteristic*Public
			gen CharacteristicIncentives=Characteristic*Incentives
		
			local lab: word `i' of `names'


			xi: areg `out' `spec`i'' & q>=3 & q<=8 & R_Gender_Followup == 0, clu(GroupNumber)a(Final_Randomization_Cluster)
		
		estadd scalar N2=e(N)

		estadd local Treatment "`lab'"
		local lab:  variable label `c' 
		estadd local sample="`lab'"
		
		sum `out' if R_Gender_Followup == 0
		estadd scalar Control_Mean=r(mean)
		estadd scalar Control_SD=r(sd)

		
		distinct Id if `out'!=. & R_Gender_Followup == 0
		estadd scalar N3=r(ndistinct)
		
		estadd local space=""


		est store E`ester'
		
		local eststr "`eststr' E`ester'"
		local ++ester
		}
		drop CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic 
		
		}
		}
		local part2=`ester'+1
esttab `eststr' using "`table'", nostar keep(CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives ///
  Characteristic  ) label noabbrev `type' replace nogaps ///
		coeflabels(CharacteristicPublicIncentives "Characteristic*Public*Incentives"  CharacteristicIncentives "Characteristic*Incentives" ///
		CharacteristicPublic "Characteristic*Public" Characteristic   "Characteristic" ///
		Public "Public" Incentives "Incentives") ///
			stats( space Control_Mean Control_SD space sample Treatment space N2 N3, fmt( 2 2 2 2 0 0) layout( @ @ [@] @ @ @) /// 
		labels(" " "Mean of" "Outcome" " " "Characteristic" "Treatment" " " "N" "No. HHs" )) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1.3\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end						
		
*** WITH GENDER INTERACTION:
cap prog drop maketable_hetero_incpub_interF
prog define maketable_hetero_incpub_interF
est clear
syntax varlist [if], [Table(string)] [MAIN(varlist)] [CONT(varlist)] [OLOGit(varlist)] [WEIGHT(string)] [TYPE(string)] [FOOTnote(string)] [PREHEAD1(string)] /// 
	[PREHEAD2(string)] [TITLE(string)] [LABel(string)]
	

	local ester=1
	local eststr ""
	tokenize `ologit'
	
	local spec1 "Characteristic  CharacteristicFemale CharacteristicPublic CharacteristicPublicF CharacteristicIncentives CharacteristicIncentivesF CharacteristicPublicIncentives CharacteristicPublicIncentivesF Public_Incentives Public Incentives R_Female i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=."
	local spec2 "Characteristic  CharacteristicFemale CharacteristicPublic CharacteristicPublicF CharacteristicIncentives CharacteristicIncentivesF CharacteristicPublicIncentives CharacteristicPublicIncentivesF Public_Incentives Public Incentives R_Female i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==1"
	local spec3 "Characteristic  CharacteristicFemale CharacteristicPublic CharacteristicPublicF CharacteristicIncentives CharacteristicIncentivesF CharacteristicPublicIncentives CharacteristicPublicIncentivesF Public_Incentives Public Incentives R_Female i.Surveyor_Code i.q  i.survey_month if Pooled_Pct!=.& Revealed==0"

	
	local names ""Pooled""Stakes"  "No Stakes""
					
	foreach out in `varlist'  {		
	
		foreach c in `main'  {	
			forvalues i=1/3{
			cap drop Characteristic  CharacteristicPublicIncentivesF CharacteristicPublicIncentives CharacteristicF CharacteristicPublic CharacteristicIncentives  CharacteristicPublicF CharacteristicIncentivesF Characteristic 
			gen Characteristic=`c'
			gen CharacteristicPublicIncentivesF=Characteristic*Public*Incentives*R_Female
			gen CharacteristicPublicIncentives=Characteristic*Public*Incentives
			gen CharacteristicPublic=Characteristic*Public
			gen CharacteristicIncentives=Characteristic*Incentives
			gen CharacteristicFemale=Characteristic*R_Female
			gen CharacteristicPublicF = Characteristic*Public*R_Female
			gen CharacteristicIncentivesF = Characteristic*Incentives*R_Female

		
			local lab: word `i' of `names'


			xi: areg `out' `spec`i'' & q>=3 & q<=8 , clu(GroupNumber)a(Final_Randomization_Cluster)
		
		estadd scalar N2=e(N)

		estadd local Treatment "`lab'"
		local lab:  variable label `c' 
		estadd local sample="`lab'"
		
		sum `out' 
		estadd scalar Control_Mean=r(mean)
		estadd scalar Control_SD=r(sd)

		
		distinct Id if `out'!=.
		estadd scalar N3=r(ndistinct)
		
		estadd local space=""


		est store E`ester'
		
		local eststr "`eststr' E`ester'"
		local ++ester
		}
		drop CharacteristicPublicIncentives CharacteristicPublic CharacteristicIncentives  Characteristic CharacteristicPublicIncentivesF CharacteristicFemale CharacteristicPublicF CharacteristicIncentivesF
		
		}
		}
		local part2=`ester'+1
esttab `eststr' using "`table'", nostar keep(CharacteristicPublicIncentivesF CharacteristicPublicIncentives CharacteristicFemale CharacteristicPublic CharacteristicIncentives CharacteristicPublicF CharacteristicIncentivesF Characteristic R_Female) label noabbrev `type' replace nogaps ///
		coeflabels(CharacteristicPublicIncentivesF "Characteristic*Public*Incentives*Female" ///		
		CharacteristicPublicIncentives "Characteristic*Public*Incentives" CharacteristicFemale "Characteristic*Female"  ///
		CharacteristicIncentives "Characteristic*Incentives" ///
		CharacteristicPublic "Characteristic*Public" Characteristic   "Characteristic" ///
		Public "Public" Incentives "Incentives" ///
		CharacteristicPublicF "Characteristic*Public*Female" CharacteristicIncentivesF "Characteristic*Incentives*Female" R_Female "Female") ///
			stats( space Control_Mean Control_SD space sample Treatment space N2 N3, fmt( 2 2 2 2 0 0) layout( @ @ [@] @ @ @) /// 
		labels(" " "Mean of" "Outcome" " " "Characteristic" "Treatment" " " "N" "No. HHs" )) ///
		nonote  eqlabels(none)  ///
		b(%9.3f) se(%9.3f) noline posthead(\hline) subs("\beta\_" "\beta_") /// 
		prehead("\begin{table}[h!] \begin{adjustbox}{max width=1.3\textwidth} \begin{threeparttable} \caption{`title'} \label{`label'} { `prehead1'`ester'`prehead2'") /// 
		postfoot(\hline \end{tabular} } \begin{tablenotes}[flushleft] \small \item `footnote' \end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table})

		end	
		